/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2019-11-27 20:14
 */
package com.yao.cloud.consumer.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.yao.cloud.consumer.util.EurekaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * 使用RestTemplate的三种方式消费服务
 * 
 * @author yaoyuming
 * @date 2019/11/27 20:14
 */
@RestController
@RequestMapping("/rest")
public class RestTemplateController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 功能描述 : 第一种HTTP方式，直接new RestTemplate()
     *
     * @param
     * @author yaoyuming
     * @date 2019/11/27
     * @return {@link String}
     */
    @RequestMapping(value = "/one", method = RequestMethod.GET)
    public String templateOne() {

        RestTemplate rt = new RestTemplate();

        String url = "http://localhost:8761/server/get";

        // 未使用负载均衡，不可写伪域名   抛异常：java.net.UnknownHostException: EUREKA-SERVER
//         String url = "http://EUREKA-SERVER/server/get";

        return rt.getForObject(url, String.class);
    }

    /**
     * 功能描述 :第二种方式，使用LoadBalancerClient
     *
     * @param
     * @author yaoyuming
     * @date 2019/11/27
     * @return {@link String}
     */
    @RequestMapping(value = "/two", method = RequestMethod.GET)
    public String templateTwo() {
        // 默认负载均衡策略为轮询
        ServiceInstance choose = loadBalancerClient.choose("EUREKA-SERVER");
        // http://ip:port/server/get
        String url = StrUtil.concat(false, "http://", choose.getHost(), ":", choose.getPort() + "", "/server/get");
        String result = new RestTemplate().getForObject(url, String.class);


        // loalBalancer其他功能
        URI uri = loadBalancerClient.reconstructURI(choose, choose.getUri());
        Console.log("choose.getUri = {}", choose.getUri());
        Console.log("uri = {}", uri);

        return result;
    }

    /**
     * 功能描述 :第三种方式，配置类对RestTemplate进行负载均衡
     *
     * @param
     * @author yaoyuming
     * @date 2019/11/27
     * @return {@link String}
     */
    @RequestMapping(value = "/thi", method = RequestMethod.GET)
    public String templateThi() {
        String url = "http://Eureka-Server/server/get";
        String forObject = restTemplate.getForObject(url, String.class);
        Console.log(forObject);
        return forObject;
    }


    /**
     * 功能描述 :封装RT请求
     *
     * @param
     * @author yaoyuming
     * @date 2019/11/27
     * @return {@link String}
     */
    @RequestMapping(value = "/four", method = RequestMethod.GET)
    public String templateFour(){
        return EurekaUtils.getServerInfo();
    }

}